<?php

/**
 * 病友圈内容表
 */

namespace app\common\model;

use MongoDB\BSON\ObjectId;
use app\common\lib\ZhyxMongo;
use catcher\exceptions\FailedException;

class WardmateCircleContent extends BaseModel
{
    protected $name = "wardmate_circle_content";

    /**
     * 获取列表
     * @param array $where 查询条件
     * @param string $field 字段
     */
    public function getLists(array $where = [], string $field = "*")
    {
        return $this->where($where)->field($field)->order('created_at', 'desc')->paginate($this->pageLimit);
    }

    /**
     * 条件获取数据
     * @param array $where 查询条件
     * @param string $field 字段
     */
    public function getOneData(array $where = [], string $field = "*")
    {
        return $this->where($where)->field($field)->find();
    }

    /**
     * 添加数据
     * @param array $param 参数
     * @return string
     */
    public function addData(array $param = []): string
    {
        //添加时销毁自带的主键id
        unset($param['id']);
        return $this->save($param);
    }

    /**
     * 更新数据
     * @param string $id 主键
     * @param array $param 参数
     * @return bool
     */
    public function saveData(string $id, array $param = []): bool
    {
        $model = self::find($id);
        return $model->save($param);
    }

    /**
     * 删除数据
     * @param string $id
     * @return bool
     */
    public function delData(string $id): bool
    {
        return self::destroy($id);
    }

    /**
     * mongo追加新元素到array类型字段
     * @param string $id 主键
     * @param string $field 字段
     * @param array $data 参数
     * @return bool
     */
    public function appToData(string $id, string $field, array $data = []): bool
    {
        try {
            ZhyxMongo::getInstance()->setCollection($this->name)->updateMany(["_id" => new ObjectId($id)], ['$push' => [$field => ['$each' => $data]]]);
            return true;
        } catch (\Exception $exception) {
            throw new FailedException($exception->getMessage());
        }
    }

    public function getListByGroupId(string $groupId, $uid): \think\Paginator
    {
        $map1 = [];
        $map[] = ["group_id", "=", $groupId];
        $map[] = ["deleted_at", "=", 0];
        if (!empty($uid)) {
            $map[] = ["uid", "=", $uid];
        }

        if (request()->userInfo["platform"] === "2") {
            $map1[] = ["visible_mode", "=", 1];
            $map1[] = ["uid", "=", (int)request()->userInfo["patientId"]];
        }
        $query = $this->where($map);
        if (!empty($map1)) {
            $query->where(function ($query) use ($map1) {
                if (!empty($map1)) {
                    $query->whereOr($map1);
                }
            });
        }
        return $query->order("_id", "desc")->paginate($this->pageLimit);


    }

    public function getCommentAttr($value, $data)
    {

        $result = (new CircleComments())->where("circle_id", "=", $data["id"])->order("created_at", "asc")->select();
        if (!empty($result)) {
            $result->append(["can_delete"]);
        }
        return $result;
    }

    public function getMidiaListAttr($value, $data)
    {
        $urlPrefix = request()->domain();
        foreach ($value as $k => &$v) {
            $v["path"] = $urlPrefix . $v["path"];
            if (!empty($v["thumb_path"])) {
                $v["thumb_path"] = $urlPrefix . $v["thumb_path"];
            }
        }
        return $value;
    }

    public function getContentById(string $contentId)
    {
        return $this->where("id", "=", new ObjectId($contentId))->find();

    }

    public function getCanDeleteAttr($value, $data): bool
    {
        $userId = request()->userInfo["platform"] === "2" ? request()->userInfo["patientId"] : request()->userInfo["id"];

        return $data["uid"] === (int)$userId;

    }

}